﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ECatalog.Models;
using ECatalog.Models.Colors;
using ECatalog.Models.UserModel;

namespace ECatalog.Controllers
{
    [Authorize]
    public class ColorController : Controller
    {
        private readonly IColorRepository colorRepository;
        private readonly IUserRepository userRepository;

        public ColorController(IColorRepository colorRepository, IUserRepository userRepository)
        {
            this.colorRepository = colorRepository;
            this.userRepository = userRepository;
        }

        [Authorize]
        [HttpPost]
        public PartialViewResult Add(Color color)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (Request.IsAjaxRequest())
            {
                if (string.IsNullOrWhiteSpace(color.RgbNumber) || string.IsNullOrWhiteSpace(color.Name))
                {
                    return PartialView(); 
                }
                color = colorRepository.Add(color);
                return PartialView("ColorItem", color);
            }
            return PartialView();
        }

        [Authorize]
        public JsonResult Remove(int colorid)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (Request.IsAjaxRequest())
            {
                colorRepository.Remove(colorid);
                return Json("ok");
            }
            return Json("error");
        }
    }
}
